//
//  SSStatusCell.swift
//  MyMicroBlog
//
//  Created by stevenSu on 15/8/1.
//  Copyright © 2015年 stevenSu. All rights reserved.
//

import UIKit
private let contentLabelFontSize: CGFloat = 15
let contentOffSet: CGFloat = 10

protocol SSStatusCellDelegate: NSObjectProtocol {
    func statusCellDidSelectLinkText(str: String)
}
class SSStatusCell: UITableViewCell {
    private let headerViewHeight: CGFloat = 55
    private let footerViewHeight: CGFloat = 44
    var pictureViewConstraintsH: NSLayoutConstraint?
    var pictureViewConstraintsW: NSLayoutConstraint?
    var footerViewConstraintsV: NSLayoutConstraint?
    var rowHeight: CGFloat?
    weak var statusCellDelegate: SSStatusCellDelegate?
    var status: SSStatus? {
        didSet {
            headerView.status = status
            let text = status?.text ?? ""
            contentLabel.attributedText =  SSEmoticonPackage.emoticonText(text, font: contentLabel.font)
            pictureView.status = status
            pictureViewConstraintsH?.constant = pictureView.bounds.height
            pictureViewConstraintsW?.constant = pictureView.bounds.width
            footerView.status = status
            footerViewConstraintsV?.constant = status?.picURLS == nil ? 0 : contentOffSet
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var headerView: SSStatusHeaderView = SSStatusHeaderView()
    lazy var contentLabel: FFLabel = {
        let lbl = FFLabel(fontColor: UIColor.darkGrayColor(), fontSize: 15)
//        lbl.textColor = UIColor.darkGrayColor()
        lbl.font = UIFont.systemFontOfSize(contentLabelFontSize)
        lbl.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * contentOffSet
        lbl.numberOfLines = 0
        lbl.labelDelegate = self
        return lbl
    }()
    lazy var pictureView: SSStatusPicView = SSStatusPicView()
    lazy var footerView: SSStatusFooterView = SSStatusFooterView()
    
    func setupUI() {
        contentView.addSubview(headerView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(footerView)
        
        headerView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: headerViewHeight))
        
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: headerView, size: nil, offset: CGPoint(x: contentOffSet, y: contentOffSet))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -contentOffSet * 2))
        
        let footCons = footerView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: footerViewHeight), offset: CGPoint(x: -contentOffSet, y: contentOffSet))
        footerViewConstraintsV = footerView.ff_Constraint(footCons, attribute: NSLayoutAttribute.Top)
//        contentView.addConstraint(NSLayoutConstraint(item: footerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
    }
    
    func getRowHeight(inputStatus: SSStatus) -> CGFloat{
        status = inputStatus
        layoutIfNeeded()
        rowHeight = CGRectGetMaxY(footerView.frame)
        return rowHeight!
    }

}

extension SSStatusCell: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        print(text)
        if text.hasPrefix("http://") {
            statusCellDelegate?.statusCellDidSelectLinkText(text)
        }
    }
}
